#include "colortablewidget.h"
#include "ui_colortablewidget.h"

#include <qrcutil.h>
#include <response.h>


template<typename T>
QList<T> convertToList(const QVariantList &variantList) {
    QList<T> intList;
    for (const QVariant &var : variantList) {
        if (var.canConvert<T>()) {
            intList.append(var.value<T>());
        }
    }
    return intList;
}

template<typename T>
QList<T> convertToList(const QVariant &variant) {
    QList<T> intList;
    for (const QVariant &var : variant.toList()) {
        if (var.canConvert<T>()) {
            intList.append(var.value<T>());
        }
    }
    return intList;
}


ColorTableWidget::ColorTableWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::ColorTableWidget)
{
    ui->setupUi(this);

    Response resp(0, QR("://color.txt"));
    auto list = resp.toList();
    auto rows = list.count();
    auto columns = 7;

    ui->tableWidget->setRowCount(rows);
    ui->tableWidget->setColumnCount(7);
    ui->tableWidget->setHorizontalHeaderLabels(QStringList() << "编号" << "名称" << "拼音" << "颜色" << "RGB" << "CMYK" << "HEX");
    ui->tableWidget->verticalHeader()->setVisible(false);

    for (int row = 0; row < rows; ++row) {
        auto items = mapToItems(list.at(row).toMap());
        for (int column = 0; column < columns; ++column) {
            ui->tableWidget->setItem(row, column, items.at(column));
        }
    }
}

ColorTableWidget::~ColorTableWidget()
{
    delete ui;
}

QList<QTableWidgetItem *> ColorTableWidget::mapToItems(const QVariantMap &map)
{
    int id = map.value("id").toInt();
    QString name = map.value("名称").toString();
    QString pinyin = map.value("拼音").toString();

    QList<int> rgb = convertToList<int>(map.value("RGB"));
    QString hex = map.value("HEX").toString();
    QList<int> cmyk = convertToList<int>(map.value("CMYK"));


    QString rgb_str = QString("rgb(%1,%2,%3)").arg(rgb[0]).arg(rgb[1]).arg(rgb[2]);
    QString cmyk_str = QString("CMYK(%1,%2,%3,%4)").arg(cmyk[0]).arg(cmyk[1]).arg(cmyk[2]).arg(cmyk[2]);

    return newItems(id, name, pinyin, QColor(hex), rgb_str, cmyk_str, hex);
}

QList<QTableWidgetItem *> ColorTableWidget::newItems(int id, QString name, QString pinyin, const QColor &color, QString RGB, QString CMYK, QString Hex)
{
    QList<QTableWidgetItem*> list;
    list << newItem(id);
    list << newItem(name);
    list << newItem(pinyin);
    list << newItem(color);
    list << newItem(RGB);
    list << newItem(CMYK);
    list << newItem(Hex);
    return list;
}

QTableWidgetItem *ColorTableWidget::newItem()
{
    return new QTableWidgetItem;
}

QTableWidgetItem *ColorTableWidget::newItem(int i)
{
    auto item = newItem();
    item->setText(QString::number(i));
    item->setTextAlignment(Qt::AlignCenter);
    return item;
}

QTableWidgetItem *ColorTableWidget::newItem(QString text)
{
    auto item = newItem();
    item->setText(text);
    return item;
}

QTableWidgetItem *ColorTableWidget::newItem(QColor color)
{
    auto item = newItem();
    item->setBackground(QBrush(color));
    return item;
}
